package kjx_test_security.auth.components;


import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 用于处理用户未登录且没有权限访问资源时，返回的响应
 **/
@Slf4j
@Configuration
public class ResetAuthenticationEntryPoint implements AuthenticationEntryPoint {



    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        int code = HttpStatus.UNAUTHORIZED.value();
        log.error("未登录用户认证失败-------认证失败: {}", e.getMessage());
        response.setCharacterEncoding("UTF-8");
        response.setStatus(code);
        response.sendError(400, HttpStatus.FORBIDDEN.getReasonPhrase());
        ServletUtil.write(response,"<body style=\"background-color: #abcdef;\">  <h1 style='color:red'>您暂无权限auth</h1></body>","text/html");
    }

}
